'use strict';
const db = uniCloud.database()
const $ = db.command.aggregate
const _ = db.command;
exports.main = async (event, context) => {
	if (event.leixing == 0) {
		const res = await db.collection('zhangwuliushui')
			.aggregate()
			.match({ //时间段
				'riqi': _.gt(event.qishitime).and(_.lt(event.jieshutime)),
				'biandong':'收入',
				'xiaoqu': _.in(event.weizhi)
			})
			.group({
				_id: '$leibie',
				total: $.sum('$jine')
			})
			.end()
		const res1 = await db.collection('zhangwuliushui')
			.aggregate()
			.match({ //时间段
				'riqi': _.gt(event.qishitime).and(_.lt(event.jieshutime)),
				'biandong':'支出',
				'xiaoqu': _.in(event.weizhi)
			})
			.group({
				_id: '$leibie',
				total: $.sum('$jine')
			})
			.end()
		const res2 = await db.collection('zhangwuliushui')
			.aggregate()
			.match({ //时间段
				'riqi': _.gt(event.qishitime).and(_.lt(event.jieshutime)),
				'biandong':'收入',
				'xiaoqu': _.in(event.weizhi)
			})
			.group({
				_id: '$biandong',
				total: $.sum('$jine')
			})
			.end()
		const res3 = await db.collection('zhangwuliushui')
			.aggregate()
			.match({ //时间段
				'riqi': _.gt(event.qishitime).and(_.lt(event.jieshutime)),
				'biandong':'支出',
				'xiaoqu': _.in(event.weizhi)
			})
			.group({
				_id: '$biandong',
				total: $.sum('$jine')
			})
			.end()
		return {
			res,
			res1,
			res2,
			res3
		}
	}else if (event.leixing == 1) {
		const dbJQL = uniCloud.databaseForJQL({
			event,
			context
		})
		const _a = dbJQL.command;
		dbJQL.setUser({ // 指定后续执行操作的用户信息，此虚拟用户将同时拥有传入的uid、role、permission
				// uid: 'user-id', // 用户id
				role: ['admin'], // 指定当前执行用户的角色为admin。如果只希望指定为admin身份，可以删除uid和permission字段
				// permission: [] // 用户权限
			})
		const order = await dbJQL.collection('xiaokelist').where({
			'xiaoketime': _a.gt(event.qishitime).and(_a.lt(event.jieshutime)),
			'xiaoqu': _a.in(event.weizhi),
			'kechengfenleiid':new RegExp(event.fenleiid) 
		}).getTemp() // 临时表field方法内需要包含关联字段，否则无法建立关联关系
		const book = await dbJQL.collection('uni-id-users').field(
				'_id,name,mobile')
		.getTemp() // 临时表field方法内需要包含关联字段，否则无法建立关联关系
		
		const res = await dbJQL.collection(order, book)
		// .where({
			// fenleiid:new RegExp(event.fenleiid)
		// })
		.groupBy('jiaoxuelaoshiid')
		.groupField('sum(yikefeiyong) as totalScore,count(*) as xiaokeshu')
		.get()
		const res1 = await dbJQL.collection('xiaokelist').where({
			'xiaoketime': _a.gt(event.qishitime).and(_a.lt(event.jieshutime)),
			'xiaoqu': _a.in(event.weizhi),
			'kechengfenleiid':new RegExp(event.fenleiid) 
		})
		.count()
		const res2 = await dbJQL.collection("laoshishangkelist").where({
			'time': _a.gt(event.qishitime).and(_a.lt(event.jieshutime)),
			'kechengxiaoqu': _a.in(event.weizhi),
			'kechengfenleiid':new RegExp(event.fenleiid) 
		})
		.groupBy('laoshiid')
		.groupField('sum(keshifeitolaoshi) as totalScore,count(*) as shangkeshu')
		.get()
		return {res,res1,res2}
	}else if (event.leixing == 2) {
		const res = await db.collection('xiaokelist')
			.aggregate()
			.match({ //时间段
				'xiaoketime': _.gt(event.qishitime).and(_.lt(event.jieshutime)),
				'jiaoxuelaoshiid':event.id,
				'xiaoqu': _.in(event.weizhi)
			})
			.group({
				_id: '$kecheng',
				total: $.sum('$yikefeiyong')
			})
			.end()
		return res
		
	}else if (event.leixing == 3) {
		const res = await db.collection('baokelist').doc(event.id).get()
		return res
	}else if (event.leixing == 4) {
		const res = await db.collection('xiaokelist').where({
			jiaoxuelaoshiid:event.jiaoxuelaoshiid,
			kecheng:event.kecheng,
			xiaoketime: _.gt(event.qishitime).and(_.lt(event.jieshutime)),
		}).get()
		return res
	}else if (event.leixing == 5) {
		const res = await db.collection('baokelist').where({
			baomingdate: _.gt(event.qishitime).and(_.lt(event.jieshutime)),
			xiaoqu: _.in(event.weizhi)
			}).count()
		const res1 = await db.collection('baokelist').where({
			zhuangtai:1,
			baomingdate: _.gt(event.qishitime).and(_.lt(event.jieshutime)),
			xiaoqu: _.in(event.weizhi)
			}).count()
		const res2 = await db.collection('baokelist').where({
			zhuangtai:0,
			baomingdate: _.gt(event.qishitime).and(_.lt(event.jieshutime)),
			xiaoqu: _.in(event.weizhi)
			}).count()
		const res3 = await db.collection('baokelist').where({
			zhuangtai:2,
			baomingdate: _.gt(event.qishitime).and(_.lt(event.jieshutime)),
			xiaoqu: _.in(event.weizhi)
			}).count()
		const res5 = await db.collection('baokelist').where({
			zhuangtai:3,
			baomingdate: _.gt(event.qishitime).and(_.lt(event.jieshutime)),
			xiaoqu: _.in(event.weizhi)
			}).count()
		const res4 = await db.collection('baokelist')
			.aggregate()
			.match({ //时间段
				'baomingdate': _.gt(event.qishitime).and(_.lt(event.jieshutime)),
				'xiaoqu': _.in(event.weizhi)
			})
			.group({
				_id: '$kecheng',
				num: $.sum(1),
				total: $.sum('$money1')
			})
			.end()
		return {res,res1,res2,res3,res4,res5}
	}else if (event.leixing == 6) {
		const res = await db.collection('baokelist')
			.aggregate()
			.match({ 
				// 'xiaoketime': _.gt(event.qishitime).and(_.lt(event.jieshutime)),
				'xiaoqu': _.in(event.xiaoqu)
			})
			.group({
				_id: '$zhuangtai',
				num: $.sum(1)
			}).sort({
				_id:1
			})
			.end()
		return res
	}
};